#!/bin/bash

set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BUILD_DIR="$DIR/build"
ARCHIVE_DIR="$DIR/archive"
ORIGIN_PATH=$PATH

###### Versions

OPENSSL=openssl-1.1.0c
NGHTTP2=nghttp2-1.40.0
CURL=curl-7.65.1

###### NDK check

if [ -z "$ANDROID_NDK" ]; then
    echo "ANDROID_NDK not defined!"
    exit 1
fi

## Build openssl/nghttp2/curl
build()
{
    ARCH=$1 # armv7, arm64
    ARCH_DIR=$2 # armeabi-v7a, arm64-v8a
    LOG="$DIR/build_$ARCH.log"

    ###### Setup dirs
    echo 'Setup dirs'
    echo 'Setup dirs' >> $LOG 2>&1

    mkdir -p $BUILD_DIR
    cd $BUILD_DIR
    mkdir -p $ARCHIVE_DIR/{include,prebuild/android/$ARCH_DIR}

    ###### Setup toolchain
    echo 'Setup toolchain'
    echo 'Setup toolchain' >> $LOG 2>&1

    _ARCH=arm
    if [ "arm64" == "$ARCH" ]; then
        _ARCH=arm64
    fi
    rm -rf $BUILD_DIR/toolchain
    bash $ANDROID_NDK/build/tools/make-standalone-toolchain.sh --arch=$_ARCH --install-dir=$BUILD_DIR/toolchain >> $LOG 2>&1

    ###### Common env settings
    echo 'Common env settings'
    echo 'Common env settings' >> $LOG 2>&1

    TOOLCHAIN="$BUILD_DIR/toolchain"
    SYSROOT="$TOOLCHAIN/sysroot"
    PREFIX="$SYSROOT/usr/local"

    export CROSS_SYSROOT="$SYSROOT"

    ###### Build OpenSSL
    echo 'Build OpenSSL'
    echo 'Build OpenSSL' >> $LOG 2>&1

    if [ "armv7" == "$ARCH" ]; then
        export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/arm-linux-androideabi/bin:$ORIGIN_PATH
    elif [ "arm64" == $ARCH ]; then
        export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/aarch64-linux-android/bin:$ORIGIN_PATH
    fi

    export FLAGS="-I$SYSROOT/usr/include"
    export CFLAGS="$FLAGS -std=c99"
    export CPPFLAGS="$FLAGS -std=c++1z"
    export LDFLAGS="-L$SYSROOT/usr/lib"

    if [ "armv7" == "$ARCH" ]; then
        export CC="$TOOLCHAIN/bin/arm-linux-androideabi-clang --sysroot=$SYSROOT"
        export CXX="$TOOLCHAIN/bin/arm-linux-androideabi-clang++ --sysroot=$SYSROOT"
    elif [ "arm64" == "$ARCH" ]; then
        export CC="$TOOLCHAIN/bin/aarch64-linux-android-clang --sysroot=$SYSROOT"
        export CXX="$TOOLCHAIN/bin/aarch64-linux-android-clang++ --sysroot=$SYSROOT"
    fi

    rm -rf $OPENSSL
    tar xf $DIR/$OPENSSL.tar.gz
    cd $OPENSSL
    if [ 'Darwin' == `uname` ]; then
        sed -i '' 's/-mandroid //g' Configurations/10-main.conf
    else
        sed -e 's/-mandroid //g' Configurations/10-main.conf
    fi
    if [ "armv7" == "$ARCH" ]; then
        ./Configure --prefix="$SYSROOT/usr/local" android no-asm no-shared no-unit-test >> $LOG 2>&1
    elif [ "arm64" == "$ARCH" ]; then
        ./Configure --prefix="$SYSROOT/usr/local" android64-aarch64 no-asm no-shared no-unit-test >> $LOG 2>&1
    fi
    make -j 8 >> $LOG 2>&1
    make install_sw >> $LOG 2>&1
    cd ..

    ###### Build NgHttp2
    echo 'Build NgHttp2' >> $LOG 2>&1
    echo 'Build NgHttp2'

    if [ "armv7" == "$ARCH" ]; then
        export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/arm-linux-androideabi/bin:$ORIGIN_PATH
    elif [ "arm64" == "$ARCH" ]; then
        export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/aarch64-linux-android/bin:$PATH
    fi

    if [ "armv7" == "$ARCH" ]; then
        export FLAGS="-I$SYSROOT/usr/include -march=armv7-a"
    elif [ "arm64" == "$ARCH" ]; then
        export FLAGS="-I$SYSROOT/usr/include"
    fi
    export CFLAGS="$FLAGS -std=c99"
    export CPPFLAGS="$FLAGS -std=c++1z"
    export LDFLAGS="-L$SYSROOT/usr/lib"

    if [ "armv7" == "$ARCH" ]; then
        export HOST="arm-linux-androideabi"
    elif [ "arm64" == "$ARCH" ]; then
        export HOST="aarch64-linux-android"
    fi

    if [ "armv7" == "$ARCH" ]; then
        export CC="$TOOLCHAIN/bin/arm-linux-androideabi-gcc"
        export CXX="$TOOLCHAIN/bin/arm-linux-androideabi-g++"
    elif [ "arm64" == "$ARCH" ]; then
        export CC="$TOOLCHAIN/bin/aarch64-linux-android-gcc"
        export CXX="$TOOLCHAIN/bin/aarch64-linux-android-g++"
    fi

    rm -rf $NGHTTP2
    tar xf $DIR/$NGHTTP2.tar.bz2
    cd $NGHTTP2
    ./configure \
        --disable-shared \
        --host=$HOST \
        --build=amd64 \
        --with-xml-prefix="$PREFIX" \
        --without-libxml2 \
        --disable-python-bindings \
        --disable-examples \
        --disable-threads \
        --disable-dependency-tracking \
        --prefix="$PREFIX" \
        CC="$CC" \
        CXX="$CXX" \
        CPPFLAGS="-fPIE -I$PREFIX/include" \
        PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig" \
        LDFLAGS="-fPIE -pie -L$PREFIX/lib" >> $LOG 2>&1
    make -j 8 >> $LOG 2>&1
    make install >> $LOG 2>&1
    cd ..

    ###### Build Curl
    echo 'Build Curl'
    echo 'Build Curl' >> $LOG 2>&1

    if [ "armv7" == "$ARCH" ]; then
        export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/arm-linux-androideabi/bin:$ORIGIN_PATH
    elif [ "arm64" == "$ARCH" ]; then
        export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/aarch64-linux-android/bin:$ORIGIN_PATH
    fi

    export FLAGS=
    export CFLAGS=
    export CPPFLAGS=
    export LDFLAGS=

    if [ "armv7" == "$ARCH" ]; then
        export CC="$TOOLCHAIN/bin/arm-linux-androideabi-clang"
        export CXX="$TOOLCHAIN/bin/arm-linux-androideabi-clang++"
    elif [ "arm64" == "$ARCH" ]; then
        export CC="$TOOLCHAIN/bin/aarch64-linux-android-clang"
        export CXX="$TOOLCHAIN/bin/aarch64-linux-android-clang++"
    fi

    rm -rf $CURL
    tar xf $DIR/$CURL.tar.gz
    cd $CURL
    ./configure \
        --host=arm-linux-androideabi \
        --prefix="$PREFIX" \
        --disable-shared \
        --enable-static \
        --with-nghttp2="$SYSROOT/usr/local" \
        --with-ssl="$SYSROOT/usr/local" \
        --with-zlib \
        --without-idn2 \
        --enable-ipv6 \
        --disable-ldap \
        --with-random=/dev/urandom >> $LOG 2>&1
    make -j 8 >> $LOG 2>&1
    make install >> $LOG 2>&1
    cd ..

    ###### Copy files
    echo 'Copy files'
    echo 'Copy files' >> $LOG 2>&1
    cp -R $SYSROOT/usr/local/include/curl $ARCHIVE_DIR/include
    cp $SYSROOT/usr/local/lib/*.a $ARCHIVE_DIR/prebuild/android/$ARCH_DIR/

    cd $DIR
}

build armv7 armeabi-v7a
build arm64 arm64-v8a
